package com.evildog.workspace.system.application.controller;

import com.evildog.workspace.common.object.PageQuery;
import com.evildog.workspace.common.result.PagedResult;
import com.evildog.workspace.common.result.Result;
import com.evildog.workspace.common.result.ResultGenerator;
import com.evildog.workspace.dependencies.mybatis.object.BaseConvert;
import com.evildog.workspace.dependencies.mybatis.object.BaseStatusParam;
import com.evildog.workspace.dependencies.mybatis.util.QueryConditionUtils;
import com.evildog.workspace.system.application.bo.RoleBO;
import com.evildog.workspace.system.application.bo.RoleListBO;
import com.evildog.workspace.system.application.bo.RolePageBO;
import com.evildog.workspace.system.application.convert.RoleConvert;
import com.evildog.workspace.system.application.model.RoleEditParam;
import com.evildog.workspace.system.application.model.RoleListParam;
import com.evildog.workspace.system.application.model.RolePageParam;
import com.evildog.workspace.system.application.model.RoleUpdateUiPermissionParam;
import com.evildog.workspace.system.application.service.RoleService;
import com.evildog.workspace.system.application.vo.RoleListVO;
import com.evildog.workspace.system.application.vo.RolePageVO;
import com.evildog.workspace.system.application.vo.RoleUiPermissionVO;
import com.evildog.workspace.system.application.vo.RoleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * role Controller
 * 角色表
 * </p>
 *
 * @author evildog
 * @since 2020-07-25
 */
@RestController
@RequestMapping("/roles")
@AllArgsConstructor
@Api(tags = "role相关接口")
public class RoleController {

    private RoleService roleService;

    @PostMapping
    @ApiOperation(value = "添加接口")
    public Result<Boolean> save(@Validated @RequestBody RoleEditParam roleEditParam) {
        return ResultGenerator.status(roleService.save(RoleConvert.INSTANCE.convert(roleEditParam)));
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除接口")
    public Result<Boolean> removeById(@PathVariable("id") Long id) {
        return ResultGenerator.status(roleService.remove(id));
    }

    @PutMapping("/{id}")
    @ApiOperation(value = "更新接口")
    public Result<Boolean> updateById(@PathVariable("id") Long id, @Validated @RequestBody RoleEditParam roleEditParam) {
        return ResultGenerator.status(roleService.updateById(id, RoleConvert.INSTANCE.convert(roleEditParam)));
    }

    @PatchMapping("/{id}/status")
    @ApiOperation(value = "更新status接口")
    public Result<Boolean> updateStatus(@PathVariable("id") Long id, @Validated @RequestBody BaseStatusParam roleParam) {
        return ResultGenerator.status(roleService.updateStatus(id, BaseConvert.INSTANCE.convert(roleParam)));
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "查询详情接口")
    public Result<RoleVO> getById(@PathVariable("id") Long id) {
        RoleBO roleBO = roleService.getDetailById(id);
        return ResultGenerator.success(RoleConvert.INSTANCE.convert(roleBO));
    }

    @GetMapping("/page")
    @ApiOperation(value = "查询角色分页接口")
    public Result<PagedResult<RolePageVO>> getPageList(RolePageParam rolePageParam, PageQuery query) {
        PagedResult<RolePageBO> rolePageBO = roleService.getPageList(
                query,
                QueryConditionUtils.getQueryCondition(rolePageParam),
                RoleConvert.INSTANCE.convert(rolePageParam));
        return ResultGenerator.success(RoleConvert.INSTANCE.convert(rolePageBO));
    }

    @GetMapping
    @ApiOperation(value = "查询角色List接口")
    public Result<List<RoleListVO>> getList(RoleListParam param) {
        List<RoleListBO> roleList = roleService.getList(
                QueryConditionUtils.getQueryCondition(param),
                RoleConvert.INSTANCE.convert(param));
        return ResultGenerator.success(RoleConvert.INSTANCE.convertVOList(roleList));
    }

    /**
     * 修改角色关联的ui权限
     *
     * @param id 角色id
     */
    @PostMapping("/{id}/ui-permissions")
    public Result<Boolean> updateUiPermissions(@PathVariable("id") Long id, @Validated @RequestBody RoleUpdateUiPermissionParam param) {
        return ResultGenerator.status(
                roleService.updateUiPermissions(id, RoleConvert.INSTANCE.convert(param))
        );
    }

    /**
     * 查询角色关联的所有ui权限
     *
     * @param id 角色id
     */
    @GetMapping("/{id}/ui-permissions")
    public Result<RoleUiPermissionVO> getAllUiPermissions(@PathVariable("id") Long id) {
        return ResultGenerator.success(RoleConvert.INSTANCE.convert(roleService.getAllUiPermissions(id)));
    }

    /**
     * 查询角色关联的所有ui权限
     */
    @GetMapping("/by-user-group")
    public Result<List<RoleListVO>> getUserRolesByUserGroup(@RequestParam("userGroupId") Long userGroupId) {
        List<RoleListBO> list = roleService.getUserRolesByUserGroup(userGroupId);
        return ResultGenerator.success(RoleConvert.INSTANCE.convertVoList(list));
    }

}